﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataLib.Accounts.Bus;
using System.Web.Script.Serialization;
using System.Text;
using log4net;

namespace Ewts.ashx
{
    /// <summary>
    /// PDBarOuts 的摘要说明
    /// </summary>
    public class PDBarOuts : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;


            string strjson = "0";

            if (context.Request["otype"] != null && context.Request["otype"].ToString().Trim() == "delBar")
            {
                string barvalue = context.Request["barvalue"];
                string operman = context.Request["oper"];
                string outid = context.Request["outid"];
                DelBarCode(barvalue, operman, outid);
            }

            //验证条码是否合法，返回JSON数组
            if (context.Request["barcode"] != null && context.Request["barcode"].ToString().Trim() != "")
            {
                string par_barcode = context.Request["barcode"].ToString().Trim();
                string par_otype = context.Request["otype"].ToString().Trim();
                string par_input = context.Request["bcity"].ToString().Trim();
                string par_output = context.Request["gcity"].ToString().Trim();
                string oldload = context.Request["oldload"].ToString().Trim();
                strjson = OutBarCodeConfirm(par_barcode, par_otype, par_input, par_output, oldload);
            }

            //出库事件提交
            if (context.Request["outbar"] != null)
            {

                strjson = SaveOutStorage(context.Request["outBar"], context.Request["outid"], context.Request["outno"], context.Request["oper"]).ToString();
            }

            context.Response.Write(strjson);
        }

        /// <summary>
        /// 删除条码
        /// </summary>
        /// <param name="barvalue"></param>
        /// <param name="operman"></param>
        /// <param name="outid"></param>
        /// <returns></returns>
        public int DelBarCode(string barvalue, string operman, string outid)
        {
            int rows = 0;
            DataLib.BLL.OutStorage bll = new DataLib.BLL.OutStorage();
            rows = bll.DelBarOut(barvalue, operman, outid);
            return rows;
        }

        /// <summary>
        /// 验证BarCode是否合法
        /// </summary>
        /// <param name="barcodevalue"></param>
        /// <param name="outType"></param>
        /// <returns>返回Json数组，Barcode、客户信息、验证结果</returns>
        public string OutBarCodeConfirm(string barcodevalue, string outType, string indepart, string outdepart, string oldload)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string isau = "0";
            string strjson = "";
            DataLib.Model.OutStorage outModel = new DataLib.Model.OutStorage();
            DataLib.BLL.OutStorage outBll = new DataLib.BLL.OutStorage();
            DataLib.Model.BarCode barModel = new DataLib.Model.BarCode();
            DataLib.BLL.BarCode barBll = new DataLib.BLL.BarCode();

            barModel = outBll.OutBarCodeConfirm(barcodevalue, outType, oldload, ref isau);
            strjson += serializer.Serialize(barModel);
            strjson += ",";
            if (barModel != null )
            {
                if(barModel.goodscode.Substring(0,1)=="D")
                    isau = "临时库物品不能盘点";
                else if(indepart != barModel.barbelong)
                    isau = "归属地不同";
                else if(outdepart != barModel.bargraphic)
                    isau = "处理地不同";
            }
            else
            {
                isau = "条码不存在";
            }
            


            strjson += ",{\"isau\":\"" + isau + "\",\"indepart\":\"" + indepart + "\",\"outdepart\":\"" + outdepart + "\"}";


            strjson = "[" + strjson + "]";
            return strjson;
        }

        /// <summary>
        /// Model对象传参
        /// </summary>
        /// <param name="arrbar"></param>
        /// <param name="outid"></param>
        /// <param name="outno"></param>
        /// <param name="oper"></param>
      
        /// <returns></returns>
        public int SaveOutStorage(object arrbar, object outid, object outno, object oper)
        {
            int rowcount = -1;
            string ono = outno.ToString();
            string strbarr = GetBarcodeValueArr(arrbar.ToString());
            if (outid != null)
            {
                DataLib.Model.OutStorage model = new DataLib.Model.OutStorage();
                DataLib.BLL.OutStorage bll = new DataLib.BLL.OutStorage();
                int outstrid = int.Parse(outid.ToString());
                if (outstrid != 0)
                {
                    model = bll.GetModel(outstrid);
                }
                else
                {
                    model.id = 0;
                }
            
                rowcount = SubmitOutStorage(strbarr, model);

            }
            return rowcount;
        }


        /// <summary>
        /// 获取出库是否成功
        /// </summary>
        /// <param name="arrbar"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public int SubmitOutStorage(string arrbar, DataLib.Model.OutStorage model)
        {
            int rowcount = -1;
            DataLib.BLL.OutStorage bll = new DataLib.BLL.OutStorage();
            rowcount = bll.PDOutStorageSubmit(model, arrbar);
            return rowcount;
        }

        public string GetBarcodeValueArr(string arrbarid)
        {
            string str = "";
            if (arrbarid != "")
            {
                string strtemp = arrbarid.Remove(arrbarid.Length - 1);
                DataTable dt = new DataTable();
                dt = new DataLib.BLL.BarCode().GetBarCodeValue(strtemp);
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        str += dt.Rows[i]["barcodevalue"].ToString();
                        str += ",";
                    }
                    str = str.Remove(str.Length - 1);
                }
            }
            return str;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}